package com.ruike.alisurface.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.bean.GoodsBean;
import com.ruike.alisurface.utils.ThreadUtils;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.DownloadUtil;
import com.voodoo.lib_frame.tip.XToastUtils;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.imageLoader.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;

public class Buhu_SlotsAdapter extends RecyclerView.Adapter<Buhu_SlotsAdapter.ViewHolder> {

    Context context;
    List<GoodsBean> list;

    public Buhu_SlotsAdapter(Context context, List<GoodsBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setData(List<GoodsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bh, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);
        ImageView imageView = holder.img_spicon;
        if (imageView != null) {
            ImageLoader.clearImage(context, imageView);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        GoodsBean item = list.get(position);
        if (!TextUtils.isEmpty(item.getProductimg())) {
            String name = item.getProductimg().substring(item.getProductimg().lastIndexOf("/"));
            File myFile = new File(Constant.path, name);
            if (!myFile.exists()) {
                // 加载图片
                ImageLoader.loadImage(context, item.getProductimg(), holder.img_spicon);
            } else {
                // 加载图片
                ImageLoader.loadImage(context, myFile.getAbsolutePath(), holder.img_spicon);
            }
        } else {
            ImageLoader.loadImage(context, R.drawable.img_zanwimg, holder.img_spicon);
        }
        holder.tv_spname.setText(item.getProductname());
        holder.tv_slotid.setText("货道：" + item.getIndex() + "号");
        holder.tv_price.setText(item.getPrice() + "");
        holder.tv_store.setText(item.getCount() + "");


        holder.tvadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.getCount() < item.getMaxcount()) {
                    item.setCount((item.getCount() + 1));
                    list.set(position, item);
                    notifyDataSetChanged();
                } else {
                    XToastUtils.getInstance().toShowXToast(context,"已到最大库存数量", 2000);
                }
            }
        });

        holder.tvsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.getCount() > 0) {
                    item.setCount((item.getCount() - 1));
                    list.set(position, item);
                    notifyDataSetChanged();
                } else {

                    XToastUtils.getInstance( ).toShowXToast(context,"库存数量已为零", 2000);

                }

            }
        });

        holder.tv_updata_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(item.getProductimg())) {
                     XToastUtils.getInstance().toShowXToast(context,"无图，无法更新图片", 2000);
                    return;
                }
                EventBus.getDefault().post(new MsgEventBus("手动图片更新", ""));
                setUpdataImg(holder.img_spicon, item.getProductimg());

            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_spname, tv_slotid, tv_price, tv_store, tv_updata_img;
        ImageView img_spicon, tvadd, tvsub;

        public ViewHolder(View convertView) {
            super(convertView);

            img_spicon = convertView.findViewById(R.id.img_spicon);
            tv_spname = convertView.findViewById(R.id.tv_spname);
            tv_slotid = convertView.findViewById(R.id.tv_slotid);
            tv_price = convertView.findViewById(R.id.tv_price);
            tv_store = convertView.findViewById(R.id.tv_store);
            tvadd = convertView.findViewById(R.id.kc_add);
            tvsub = convertView.findViewById(R.id.kc_sub);
            tv_updata_img = convertView.findViewById(R.id.tv_updata_img);
        }

    }

    public void setUpdataImg(ImageView img, String str) {
        String name = str.substring(str.lastIndexOf("/"));
//        File myFile = new File(Constant.path, name);

        DownloadUtil.getInstance().download(str, Constant.path, name, new BaseView() {
            @Override
            public void onStarted(String loadingStr) {
                L.i("下载图片- 可以下载 " + loadingStr);
            }

            @Override
            public void onLoading(long total, long current) {

            }

            @Override
            public void onComplete() {

            }

            @Override
            public void onErrorTip(String errorStr) {

            }

            @Override
            public void onSuccess(String requestUrl, String requestJsonStr) {

            }

            @Override
            public void onFailure(String requestUrl, String requestMsg) {

            }

            @Override
            public void onError(String requestUrl, String errorMsg) {

            }

            @Override
            public void onError(String requestUrl, String errorMsg, String data) {

            }

            @Override
            public void onException(Exception exception) {

            }

            @Override
            public void onDownloadSuccess(String requestUrl, File file) {
                L.i("下载图片- 可以下载 " + file.getAbsolutePath());
                ThreadUtils.postUI(new Runnable() {
                    @Override
                    public void run() {
                        ImageLoader.loadImage(context, file.getAbsolutePath(), img);
                    }
                });
            }

            @Override
            public void onDownloading(String requestUrl, long total, long progress) {
//                L.i("下载图片- 可以下载 " + total + "==" + progress);
            }

            @Override
            public void onDownloadFailed(String requestUrl, Exception e) {
                L.i("下载图片- 可以下载 " + requestUrl + "==" + e.getMessage());
                EventBus.getDefault().post(new MsgEventBus("手动图片更新失败", ""));
            }
        });
    }
}
